package org.nlp2rdf.navigator.client.functionality;

import java.util.Iterator;
import java.util.Map;

import org.nlp2rdf.navigator.client.Application;
import org.nlp2rdf.navigator.client.common.ExampleDTO;
import org.nlp2rdf.navigator.client.util.ZebraLayouter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ExampleWidget extends FlexTable {
	final private VerticalPanel content = new VerticalPanel();
	final private HorizontalPanel buttons = new HorizontalPanel();

	// final private ExampleDTO exampleDTO;
	// private int button = 1;

	public static void addExamplesToSelectionTable(final Application app,
			final Map<String, ExampleDTO> exampleDTOs, final VerticalPanel table) {
		if (exampleDTOs.size() == 0) {
			table.add(new Label("empty"));
		}

		ZebraLayouter z = new ZebraLayouter();
		for (Iterator<ExampleDTO> it = exampleDTOs.values().iterator(); it.hasNext();) {
			ExampleDTO exampleDTO = it.next();

			if (exampleDTO == null) {
				Window.alert("The search could not be displayed");
			}

			final ExampleWidget oneRow = new ExampleWidget(exampleDTO);
			final String uri = exampleDTO.getUri();

			Button toPositive = new Button("+");
			toPositive.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent arg0) {
					app.moveToPos(exampleDTOs.get(uri));
					exampleDTOs.remove(uri);
					table.remove(oneRow);
				}
			});
			Button toNegative = new Button("‒");
			toNegative.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent arg0) {
					app.moveToNeg(exampleDTOs.get(uri));
					exampleDTOs.remove(uri);
					table.remove(oneRow);
				}
			});

			oneRow.addButton(toPositive);
			oneRow.addButton(toNegative);
			table.add(oneRow);

			z.addWidget(oneRow);
		}

	}
	
	public static void addExamplesToPosNegTable(final Map<String, ExampleDTO> exampleDTOs, final VerticalPanel table) {
		if (exampleDTOs.size() == 0) {
			table.add(new Label("empty"));
		}

		ZebraLayouter z = new ZebraLayouter();
		for (Iterator<ExampleDTO> it = exampleDTOs.values().iterator(); it.hasNext();) {
			ExampleDTO exampleDTO = it.next();

			if (exampleDTO == null) {
				Window.alert("One example is not set correctly");
			}

			final ExampleWidget oneRow = new ExampleWidget(exampleDTO);
			final String uri = exampleDTO.getUri();
			
			Button removeButton = new Button("x");
			removeButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent arg0) {
					exampleDTOs.remove(uri);
					table.remove(oneRow);
				}
			});
			oneRow.addButton(removeButton);
			table.add(oneRow);

			z.addWidget(oneRow);
		}

	}
	

	public ExampleWidget(ExampleDTO exampleDTO) {
		// this.exampleDTO = exampleDTO;

		Label html = new HTML(formatHTML(exampleDTO));

		html.setWidth("100%");
		content.setWidth("100%");
		content.add(html);
		this.setWidget(0, 0, content);
		this.setWidget(0, 1, buttons);
	}

	public void addButton(Button button) {
		this.buttons.add(button);
	}

	public String formatHTML(ExampleDTO exampleDTO) {
		StringBuffer text = new StringBuffer();
		if (exampleDTO.getLabel() != null) {
			text.append("<b>" + exampleDTO.getLabel() + "</b> <a href=\"" + exampleDTO.getUri()
					+ "\" target=\"_blank\" >more...</a><br>");
		}
		if (exampleDTO.getComment() != null) {
			text.append(exampleDTO.getComment());
		}
		return text.toString();

	}

}
